package com.kye.express.business.order.order_address;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityAddressListBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.order.OrderAddresses;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

public class AddressListActivity extends BaseAppCompatActivity implements AddressListContract.AddressListView {

    private static final String KEY_GUID = "KEY_GUID";
    public static final int ADDRESS_LIST_ACTIVITY_REQUEST_CODE = 3;
    public static final int ADDRESS_LIST_ACTIVITY_RESULT_CODE = 3;
    public static final String KEY_ADDRESS = "KEY_ADDRESS";

    private ActivityAddressListBinding mBinding;
    private MultiTypeAdapter mAdapter;
    private AddressListContract.AddressListPresenter mPresenter;
    private Dialog mDialog;
    private CompanyViewBinder mCompanyViewBinder;
    private AddressViewBinder mAddressViewBinder;

    public static void startActivityForResult(Activity activity, String guid) {
        activity.startActivityForResult(
                new Intent(activity, AddressListActivity.class)
                        .putExtra(KEY_GUID, guid),
                ADDRESS_LIST_ACTIVITY_REQUEST_CODE
        );
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_address_list);

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mBinding.recyclerViewContent.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MultiTypeAdapter();
        mCompanyViewBinder = new CompanyViewBinder();
        mAdapter.register(OrderAddresses.class, mCompanyViewBinder);
        mAddressViewBinder = new AddressViewBinder();
        mAdapter.register(AddressViewBinder.AddressViewBean.class, mAddressViewBinder);
        mBinding.recyclerViewContent.setAdapter(mAdapter);
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        mCompanyViewBinder.setOnItemClickListener(new OnItemClickListener<OrderAddresses>() {
            @Override
            public void onClickItem(OrderAddresses item, View view) {
                AddressEditActivity.startActivityForResult(AddressListActivity.this, item);
            }
        });

        mAddressViewBinder.setOnItemClickListener(new OnItemClickListener<OrderAddresses.Address>() {
            @Override
            public void onClickItem(OrderAddresses.Address item, View view) {
                setResult(ADDRESS_LIST_ACTIVITY_RESULT_CODE, new Intent().putExtra(KEY_ADDRESS, item));
                finish();
            }
        });

        mAddressViewBinder.setOnEditItemClickListener(new OnItemClickListener<OrderAddresses.Address>() {
            @Override
            public void onClickItem(OrderAddresses.Address item, View view) {
                AddressEditActivity.startActivityForResult(AddressListActivity.this, item);
            }
        });
    }

    private void initData() {
        mPresenter = new AddressListContract.AddressListPresenter();
        mPresenter.bind(this);

        mPresenter.getAddresses(getIntent().getStringExtra(KEY_GUID));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AddressEditActivity.ADDRESS_EDIT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            mPresenter.getAddresses(getIntent().getStringExtra(KEY_GUID));
        }
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onLoading() {
        mDialog = KyeProgressDialog.show(this, mDialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(mDialog);
    }

    @Override
    public void onAddress(List<Object> objects) {
        mAdapter.setItems(objects);
        mAdapter.notifyDataSetChanged();
    }
}
